package ru.urfc.portal.ui.client.widgets;

import java.util.List;

import ru.urfc.portal.ui.client.BaseDocument;
import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.Utils;
import ru.urfc.portal.ui.client.controllers.ItemsController;
import ru.urfc.portal.ui.client.controllers.events.PostEvent;
import ru.urfc.portal.ui.client.controllers.events.PutEvent;
import ru.urfc.portal.ui.client.controllers.loaders.LoaderWithPaging;
import ru.urfc.portal.ui.client.controllers.loaders.LoaderWithPagingListenter;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;

public class DocumentButtons extends BaseGooglet {
	
	interface Binder extends UiBinder<Widget, DocumentButtons> {}
	private static Binder binder = GWT.create(Binder.class);
	boolean isAfterSave = false;

	public Type<ItemsController> SAVE = new Type<ItemsController>();
	public Type<ItemsController> SEND = new Type<ItemsController>();
	public Type<ItemsController> VIEW = new Type<ItemsController>();
	
	@UiField CheckBox signed;
	@UiField Button save;
	@UiField Button send;
	@UiField Button view;
	
	@UiHandler("save") 
	void save(ClickEvent e) {
		getEventsDispatcher().fireEvent(new PutEvent(SAVE));
	}
	
	@UiHandler("send") 
	void send(ClickEvent e) {
		getEventsDispatcher().fireEvent(new PostEvent(SEND));
	}
	
	@UiHandler("view") 
	void view(ClickEvent e) {
		String url = Utils.createRestUrl(CLIENTS, getClientId(), DOCUMENTS, getItemId(), DOCUMENT_PRINT);
		Window.open(url, "_blank", "");
	}
	
	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
	}

	public void bind(final BaseDocument document) {
		ItemsController documentController = document.getController();
		register(SAVE, documentController);
		register(SEND, documentController);
		
		LoaderWithPaging documentLoader = documentController.getLoader();
		documentLoader.addLoadListener(new LoaderWithPagingListenter() {
			@Override
			public void onLoad(List<ModelData> data) {
				if (isAfterSave) {
					showInfoTip("Данные сохранены");
				} else {
					isAfterSave = true;
				}
				if (document.isDraft()) {
					signed.setEnabled(true);
					save.setEnabled(true);
				} else {
					signed.setEnabled(false);
					save.setEnabled(false);
					send.setEnabled(false);
				}
				
			}
		});		
		signed.addListener(Events.Change, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent event) {
				send.setEnabled(signed.getValue());
			}
		});

	}
	
}
